﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using Model;

namespace DAL
{
    public class AuctionDAL
    {
        DBHelper db = new DBHelper();
        /// <summary>
        /// 显示数据
        /// </summary>
        /// <returns></returns>
        public List<Auction> auctions()
        {
            string sqlShow = "select AuctionID, AuctionName, AuctionPicture, AuctionIntroduce, AuctionPrice, AuctionState, AuctionCode,AuctionCategory, AuctionUpload, AuctionAuction from [dbo].[Auction]";
            List<Auction> list = null;
            DataTable dt = db.GetData(sqlShow);
            if (dt.Rows.Count>0)
            {
                list = new List<Auction>();
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    Auction ac = new Auction();
                    ac.AuctionID = Convert.ToInt32(dt.Rows[i]["AuctionID"]);
                    ac.AuctionName = dt.Rows[i]["AuctionName"].ToString();
                    ac.AuctionPicture = dt.Rows[i]["AuctionPicture"].ToString();
                    ac.AuctionIntroduce = dt.Rows[i]["AuctionIntroduce"].ToString();
                    ac.AuctionPrice = Convert.ToDecimal(dt.Rows[i]["AuctionPrice"].ToString());
                    ac.AuctionState = dt.Rows[i]["AuctionState"].ToString();
                    ac.AuctionCode = Convert.ToInt32(dt.Rows[i]["AuctionCode"].ToString());
                    ac.AuctionCategory = Convert.ToInt32(dt.Rows[i]["AuctionCategory"].ToString());
                    ac.AuctionUpload = Convert.ToDateTime(dt.Rows[i]["AuctionUpload"].ToString());
                    ac.AuctionAuction = Convert.ToDateTime(dt.Rows[i]["AuctionAuction"].ToString());
                    list.Add(ac);
                }
            }
            return list;
        }
        /// <summary>
        /// 添加数据
        /// </summary>
        /// <param name="au"></param>
        /// <returns></returns>
        public bool Inset(Auction au)
        {
            string Sqlinsert = $"insert into [dbo].[Auction](AuctionName, AuctionPicture, AuctionIntroduce, AuctionPrice, AuctionState, AuctionCode,AuctionCategory, AuctionUpload, AuctionAuction )values('{au.AuctionName}','{au.AuctionPicture}','{au. AuctionIntroduce}','{au.AuctionPrice}','{au.AuctionState}','{au.AuctionCode}','{au.AuctionCategory}','{au.AuctionUpload}','{au.AuctionAuction}')";
            if (db.SetData(Sqlinsert))
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 修改数据
        /// </summary>
        /// <param name="au"></param>
        /// <returns></returns>
        public bool UpdateAuc(Auction au)
        {
            string UpdateSql = $"update Auction set AuctionName='{au.AuctionName}' ,AuctionPicture='{au.AuctionPicture}',AuctionIntroduce='{au.AuctionIntroduce}',AuctionPrice='{au.AuctionPrice}',AuctionState='{au.AuctionState}',AuctionCode='{au.AuctionCode}',AuctionCategory='{au.AuctionCategory}',AuctionUpload='{au.AuctionUpload}',AuctionAuction='{au.AuctionAuction}' where AuctionID='{au.AuctionID}'";
            if (db.SetData(UpdateSql))
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 删除数据
        /// </summary>
        /// <param name="au"></param>
        /// <returns></returns>
        public bool DeleteAue(Auction au)
        {
            string DeleteAu = $"delete Auction where AuctionID='{au.AuctionID}'";
            if (db.SetData(DeleteAu))
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
}
